{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 作业二：\n",
    "#### 为MNIST数据集构建一个分类器，并在测试集上达成超过90%的精度 \n",
    "提示：KNeighborsClassifier对这个任务非常有效，你只需要找到合适的超参数即可，可对weights和n_neighbors这两个超参数进行网格搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\00-app\\python\\python38\\lib\\site-packages\\sklearn\\feature_extraction\\text.py:17: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.9 it will stop working\n",
      "  from collections import Mapping, defaultdict\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.datasets import fetch_mldata\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.metrics import confusion_matrix\n",
    "from sklearn.model_selection import cross_val_score\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.metrics import precision_score, recall_score\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "\n",
    "%matplotlib inline\n",
    "mnist = fetch_mldata('MNIST original', data_home='./')   # 读取数据\n",
    "X,y = mnist['data'], mnist['target']\n",
    "\n",
    "# X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[0:60000], y[60000:]   # 划分训练集、测试集\n",
    "# shuffle_index = np.random.permutation(60000)   \n",
    "\n",
    "# 鉴于自己的电脑运算太慢，拿600条数据来做练习\n",
    "X_train, X_test, y_train, y_test = X[:600], X[600:], y[0:600], y[600:]   # 划分训练集、测试集\n",
    "shuffle_index = np.random.permutation(600)   \n",
    "\n",
    "X_train,  y_train  = X_train[shuffle_index], y_train[shuffle_index]\n",
    "y_train_9 = (y_train == 9)\n",
    "y_test_9 = (y_test == 9)\n",
    "\n",
    "# 查看数据集是不是划分的正确\n",
    "def show_img1(X,i):   \n",
    "    some_digit = X[i]\n",
    "    some_digit_image = some_digit.reshape(28,28)\n",
    "    plt.imshow(some_digit_image, cmap=matplotlib.cm.binary)\n",
    "    plt.show()\n",
    "def show_img2(X,i):\n",
    "    some_digit = X[i]\n",
    "    some_digit_image = some_digit.reshape(28,28)\n",
    "    plt.imshow(some_digit_image, cmap=matplotlib.cm.binary, interpolation='nearest')\n",
    "    plt.axis('off')\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train[10:20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAOyElEQVR4nO3df4wUdZrH8c9znCNGwV+MDHHJzYKQnJocuynxFN14Gozwh7ABT4gSz1+zMZBgsn9ouJglJiaEnLtu4rlx9sTFY09iEAOJ6GlGE2JiFlvDIYgnns7tsqLMxBgxBj3kuT+mvIzY/e2mu7qruef9SibdXU/V1JMKH6qnv131NXcXgP///qLsBgB0BmEHgiDsQBCEHQiCsANB/GUndzZlyhTv7+/v5C6BUIaHhzU6OmrVai2F3cxukPRrSRMk/Yu7r0ut39/fr0ql0souASRkWVaz1vTbeDObIOmfJS2QdLGk5WZ2cbO/D0B7tfI3+1xJ77v7B+7+taTNkhYV0xaAorUS9gsl/Wnc64P5su8wswEzq5hZZWRkpIXdAWhFK2Gv9iHA97576+6D7p65e9bb29vC7gC0opWwH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4f5Y+HJT2n7puK+pNvZ9DNHw+X3M//6aZpvKtNM64uOHZlTn9eRtjfkDTLzH5oZj2SlknaXkIf32NmZ+YfnMjMzpR0vbpvKurtkm7Ln98maVuJvXxHt0zjXWuacZV87Eqf/tzdO/4jaaHGPpH/L0n/WEYPNfqaIek/8p99Zfcm6WmNva37H429I7pT0vmShiQdyB/P66Le/lXS25L2aCxY00rq7SqN/Wm4R9Lu/Gdh2ccu0VdHjhtflwWC4Bt0QBCEHQiCsANBEHYgCMIOBEHYgSAIOxDE/wJ6+WDILbkGyQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "show_img1(X,28)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAOh0lEQVR4nO3df6xU9ZnH8c8DgkGLiuXyI9SVbv2BZnWpTmCNBl0bG9FENKYb+QNpgt7GSGxNTVarsUb/EDfbNhuzAakiaFixprJcjcGCEgUTCaNBQHHFNXdb9ObeSzRya4xFefaPe2hu4c53LnPOzBl43q9kMjPnmTPnydz7mTNzzpnzNXcXgOPfqLIbANAahB0IgrADQRB2IAjCDgRxQisXNnHiRJ8+fXorFwmE0t3drX379tlwtVxhN7OrJf2HpNGSHnf3JanHT58+XdVqNc8iASRUKpWatYY/xpvZaEn/KWmupPMlzTez8xt9PgDNlec7+yxJH7r7R+7+F0lrJM0rpi0ARcsT9mmS/jTk/t5s2t8ws04zq5pZtb+/P8fiAOSRJ+zDbQQ44thb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hRGZ2sqQfqv2Gou6StDC7vVDSuhJ7+RvtMox3rWHGVfJrV/rw5+7e8oukazS4Rf5/Jd1bRg81+vp7Se9kl3fL7k3SMxr8WHdAg5+IFkn6tqRXJO3Jrk9vo96elrRT0g4NBmtqSb1dpsGvhjskbc8u15T92iX6asnrxuGyQBAcQQcEQdiBIAg7EARhB4Ig7EAQhB0IgrADQfw/2Sh2Lk9VZeQAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "show_img1(X_train,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAOcAAADnCAYAAADl9EEgAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAGEklEQVR4nO3dPWiUaRuG4S+isqhJLzYiKDYpbESUNIKNhWgj/iIIYqkWohbaiZXWIlYKFtpYSEgjRlGwMNjEKkEMiBAF/xu1yNYfm7nH5J1MrkyOo/RiZp4tTh7Yl5n0zczM/A/Is2KxDwDMTpwQSpwQSpwQSpwQamWb3f/KhYXXN9s/ujkhlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDghlDgh1MrFPgBz8/79+3I/fPhwuT9//ryTx+maZ8+elfvQ0FCXTtI9bk4IJU4IJU4IJU4IJU4IJU4IJU4I5TlnmN+/f5f78ePHy/3Fixfl3tfXN+czJViq527CzQmhxAmhxAmhxAmhxAmhxAmhPEpZBB8+fGi5HTt2rHzt06dPO32cvzYwMFDu27ZtK/exsbFy//nz55zP1MvcnBBKnBBKnBBKnBBKnBBKnBBKnBDKc84F8O3bt3I/ceJEy210dLTDp+mcrVu3lvuTJ0/KfXBwsNzfvHkz5zP1MjcnhBInhBInhBInhBInhBInhBInhPKccx4+fvxY7uPj4+X++PHjTh5nTvr7+8t948aNLbfbt2+Xr52eni73X79+lTv/z80JocQJocQJocQJocQJocQJocQJoTznnIdbt26V+5UrV7p0krnbtWtXuQ8PD8/7vS9fvlzuk5OT837v5cjNCaHECaHECaHECaHECaHECaHECaE855yHgwcPlvvdu3fLfWJiYt6fvWbNmnK/evVque/du3fen013uTkhlDghlDghlDghlDghlDghlEcp83D//v1yb/KopJ19+/aV+5kzZxbss9v9JOjY2NiCffZy5OaEUOKEUOKEUOKEUOKEUOKEUOKEUJ5zhhkYGCj3S5cudekk/zU1NVXuIyMjjd5/1apVLbcVK5bfPbL8/othiRAnhBInhBInhBInhBInhBInhPKccxZfv34t9/Hx8Ubvv2XLlpbbgwcPytcODg42+uxk1XdRd+7c2cWTZHBzQihxQihxQihxQihxQihxQihxQijPOWdx7ty5cm/3u7Xt7Nmzp+XWy88xmRs3J4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4Rals85L168WO537txp9P4bNmwo9xs3bjR6/6Vq7dq15b558+YunWRpcHNCKHFCKHFCKHFCKHFCKHFCqGX5KOXPnz/lPjMz0+j9T58+Xe7Vn7pLdvPmzUavX79+fbmfOnWq0fv3GjcnhBInhBInhBInhBInhBInhBInhOrZ55w/fvxouU1PTzd677Nnz5b7hQsXGr3/Ynr37l3L7dGjR907CG5OSCVOCCVOCCVOCCVOCCVOCCVOCNWzzzlfv37dcrt3716j9163bl25J39fc2JiotyPHDnScvv06VOjzz569Gij1y83bk4IJU4IJU4IJU4IJU4IJU4IJU4I1bPPORfSly9fyn1ycrJLJ/mv69evl/vIyEi5T01Ntdz++eef8rXtvse6lL/nuhjcnBBKnBBKnBBKnBBKnBBKnBCqr82fu2v2t/AW0du3b1tuhw4dKl/76tWrTh+nJ2zfvr3cX7582aWT9Jy+2f7RzQmhxAmhxAmhxAmhxAmhxAmhxAmhevYrY5s2bWq5DQ0Nla/t5eecq1evLvf+/v6W27Vr1zp9HApuTgglTgglTgglTgglTgglTgglTgjVs885K+2+l7h///5yf/jwYSeP01W7d+8u9+Hh4S6dhHbcnBBKnBBKnBBKnBBKnBBKnBBKnBCqZ3+3tonv37+X++joaLl//vy53E+ePDnXI/218+fPl/uBAwfKfceOHZ08Dn/H79bCUiJOCCVOCCVOCCVOCCVOCCVOCOU5Jyw+zzlhKREnhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhFrZZp/1J/uAhefmhFDihFDihFDihFDihFDihFD/Anv41QDLBfBFAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "show_img2(X,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAOcAAADnCAYAAADl9EEgAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAGwUlEQVR4nO3dTYiNfx/H8TP80ShPC1IWLKSxIPEvKVFqFlIkGxZYeChlo2YjZcECaxvKU1lJUWyISCiy8pARGxvJQ8lIkuLe3Zt7zvdkzsw9nzPzei19umauMm9X+XXm6vrz508DyDNhtG8AGJw4IZQ4IZQ4IZQ4IdQ/LXb/lQsjr2uwP/TkhFDihFDihFDihFDihFDihFDihFDihFDihFDihFDihFDihFDihFDihFDihFDihFDihFDihFDihFDihFDihFDihFDihFDihFDihFDihFDihFDihFDihFDihFC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fU9/QWvAIROIk4IJU4IJU4IJU4IJU4IJU4I5ZxzEL9//y73gYGBcp82bdpf3xMja/369eXe6vfitvoMb5ucc0InESeEEieEEieEEieEEieEGrO/GhP+Rnd392jfwv/w5IRQ4oRQ4oRQ4oRQ4oRQ4oRQ4oRQzjmH4OfPn+XuI2P/f61+teXbt2/L/cCBA8N4N8PDkxNCiRNCiRNCiRNCiRNCiRNCiRNCjctzzj179pT76dOny/3EiRNt7RMm+DdxuF28eLHcX716Ve4rVqwYztsZFn5KIJQ4IZQ4IZQ4IZQ4IZQ4IZQ4IdS4fAXgp0+fyn3evHnl3up1cI8ePSr3lStXlvt4Vb168cqVK+W1e/fuLfdWf6d37twp99mzZ5d7m7wCEDqJOCGUOCGUOCGUOCGUOCHUuDxKaWXfvn3lfurUqXKfNWtWuff39zfd5syZU17b1TXo/7pHqI5CGo1G49mzZ+V+/PjxptulS5fKa3t6esq91fVLly4t9xHmKAU6iTghlDghlDghlDghlDghlDghlHPOIWj3HLSydevWct++fXu59/b2lvvXr1/L/caNG023ly9fltc+efKk3G/fvl3ulQ0bNpT7+fPny32EP/LVLuec0EnECaHECaHECaHECaHECaHECaGcc46AkTwH7WTr1q0r9x07djTddu7cOdy3k8Q5J3QScUIocUIocUIocUIocUIocUIo55ww+pxzQicRJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4T6p8U+6KvJgJHnyQmhxAmhxAmhxAmhxAmhxAmh/gO71ykXq0kAwwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "show_img2(X_train,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(784,)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据预处理，看看大概有多少组数据\n",
    "test_digit = X_train[10]\n",
    "test_digit.reshape(1,784)\n",
    "test_digit.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 784)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据预处理，看看大概有多少组数据\n",
    "test_digit = X_train[13:14]\n",
    "test_digit.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_digits = X_train[10:20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 78 ms\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
       "           metric_params=None, n_jobs=1, n_neighbors=5, p=2,\n",
       "           weights='uniform')"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "knn_clf = KNeighborsClassifier()  # 来实例化K邻这个分类方法 \n",
    "knn_clf.fit(X_train, y_train_9)  # 训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False, False, False, False, False, False, False,\n",
       "       False])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn_clf.predict(test_digits) # 拿测试集来预测一下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn_clf.predict(test_digit) # 拿测试集来预测一下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 918 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "train_score = cross_val_score(knn_clf, X_train, y_train_9, cv=5, scoring='accuracy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=1)]: Done   1 out of   1 | elapsed:    0.1s remaining:    0.0s\n",
      "[Parallel(n_jobs=1)]: Done   2 out of   2 | elapsed:    0.2s remaining:    0.0s\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 807 ms\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=1)]: Done   5 out of   5 | elapsed:    0.7s finished\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "from sklearn.model_selection import cross_val_predict\n",
    "y_train_pred = cross_val_predict(knn_clf, X_train, y_train_9, cv=5, verbose=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 998 µs\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[600]], dtype=int64)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "confusion_matrix(y_train_9, y_train_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "精度：0.00 %\n",
      "召回率：0.00 %\n"
     ]
    }
   ],
   "source": [
    "print('精度：{0:.2f} %'.format(100*precision_score(y_train_9, y_train_pred)))\n",
    "print('召回率：{0:.2f} %'.format(100*recall_score(y_train_9, y_train_pred)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 2min 29s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "y_test_pred = knn_clf.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[62442,     0],\n",
       "       [ 6958,     0]], dtype=int64)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "confusion_matrix(y_test_9, y_test_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "精度：0.00 %\n",
      "召回率：0.00 %\n"
     ]
    }
   ],
   "source": [
    "print('精度：{0:.2f} %'.format(100*precision_score(y_train_9, y_train_pred)))\n",
    "print('召回率：{0:.2f} %'.format(100*recall_score(y_train_9, y_train_pred)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 5 folds for each of 6 candidates, totalling 30 fits\n",
      "[CV] n_neighbors=2, weights=uniform ..................................\n",
      "[CV] ........ n_neighbors=2, weights=uniform, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=2, weights=uniform ..................................\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=1)]: Done   1 out of   1 | elapsed:    0.6s remaining:    0.0s\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[CV] ........ n_neighbors=2, weights=uniform, score=1.0, total=   0.0s\n",
      "[CV] n_neighbors=2, weights=uniform ..................................\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=1)]: Done   2 out of   2 | elapsed:    1.4s remaining:    0.0s\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[CV] ........ n_neighbors=2, weights=uniform, score=1.0, total=   0.0s\n",
      "[CV] n_neighbors=2, weights=uniform ..................................\n",
      "[CV] ........ n_neighbors=2, weights=uniform, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=2, weights=uniform ..................................\n",
      "[CV] ........ n_neighbors=2, weights=uniform, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=2, weights=distance .................................\n",
      "[CV] ....... n_neighbors=2, weights=distance, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=2, weights=distance .................................\n",
      "[CV] ....... n_neighbors=2, weights=distance, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=2, weights=distance .................................\n",
      "[CV] ....... n_neighbors=2, weights=distance, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=2, weights=distance .................................\n",
      "[CV] ....... n_neighbors=2, weights=distance, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=2, weights=distance .................................\n",
      "[CV] ....... n_neighbors=2, weights=distance, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=4, weights=uniform ..................................\n",
      "[CV] ........ n_neighbors=4, weights=uniform, score=1.0, total=   0.0s\n",
      "[CV] n_neighbors=4, weights=uniform ..................................\n",
      "[CV] ........ n_neighbors=4, weights=uniform, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=4, weights=uniform ..................................\n",
      "[CV] ........ n_neighbors=4, weights=uniform, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=4, weights=uniform ..................................\n",
      "[CV] ........ n_neighbors=4, weights=uniform, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=4, weights=uniform ..................................\n",
      "[CV] ........ n_neighbors=4, weights=uniform, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=4, weights=distance .................................\n",
      "[CV] ....... n_neighbors=4, weights=distance, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=4, weights=distance .................................\n",
      "[CV] ....... n_neighbors=4, weights=distance, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=4, weights=distance .................................\n",
      "[CV] ....... n_neighbors=4, weights=distance, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=4, weights=distance .................................\n",
      "[CV] ....... n_neighbors=4, weights=distance, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=4, weights=distance .................................\n",
      "[CV] ....... n_neighbors=4, weights=distance, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=6, weights=uniform ..................................\n",
      "[CV] ........ n_neighbors=6, weights=uniform, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=6, weights=uniform ..................................\n",
      "[CV] ........ n_neighbors=6, weights=uniform, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=6, weights=uniform ..................................\n",
      "[CV] ........ n_neighbors=6, weights=uniform, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=6, weights=uniform ..................................\n",
      "[CV] ........ n_neighbors=6, weights=uniform, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=6, weights=uniform ..................................\n",
      "[CV] ........ n_neighbors=6, weights=uniform, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=6, weights=distance .................................\n",
      "[CV] ....... n_neighbors=6, weights=distance, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=6, weights=distance .................................\n",
      "[CV] ....... n_neighbors=6, weights=distance, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=6, weights=distance .................................\n",
      "[CV] ....... n_neighbors=6, weights=distance, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=6, weights=distance .................................\n",
      "[CV] ....... n_neighbors=6, weights=distance, score=1.0, total=   0.1s\n",
      "[CV] n_neighbors=6, weights=distance .................................\n",
      "[CV] ....... n_neighbors=6, weights=distance, score=1.0, total=   0.1s\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=1)]: Done  30 out of  30 | elapsed:   23.8s finished\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=5, error_score='raise',\n",
       "       estimator=KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
       "           metric_params=None, n_jobs=1, n_neighbors=5, p=2,\n",
       "           weights='uniform'),\n",
       "       fit_params=None, iid=True, n_jobs=1,\n",
       "       param_grid=[{'weights': ['uniform', 'distance'], 'n_neighbors': [2, 4, 6]}],\n",
       "       pre_dispatch='2*n_jobs', refit=True, return_train_score='warn',\n",
       "       scoring=None, verbose=3)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 由于自己电脑性能问题这里的执行可能跑几天（电脑性能时老师的4/1），这里就不跑了\n",
    "# %%time\n",
    "param_grid = [{'weights':[\"uniform\", \"distance\"], 'n_neighbors':[2, 4, 6]}]\n",
    "knn_clf = KNeighborsClassifier()\n",
    "grid_search = GridSearchCV(knn_clf, param_grid, cv=5, verbose=3)\n",
    "grid_search.fit(X_train, y_train_9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'n_neighbors': 2, 'weights': 'uniform'}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid_search.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
       "           metric_params=None, n_jobs=1, n_neighbors=2, p=2,\n",
       "           weights='uniform')"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid_search.best_estimator_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid_search.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid_search.predict(test_digit)  # 预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 1min 51s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "y_test_pred = grid_search.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[62442,     0],\n",
       "       [ 6958,     0]], dtype=int64)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "confusion_matrix(y_test_9, y_test_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "精度：0.00 %\n",
      "召回率：0.00 %\n"
     ]
    }
   ],
   "source": [
    "print('精度：{0:.2f} %'.format(100*precision_score(y_train_9, y_train_pred)))\n",
    "print('召回率：{0:.2f} %'.format(100*recall_score(y_train_9, y_train_pred)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里主要使用了KNN算法去实现更精准的分类器，使用gridsearchvc网格搜索，查找出最佳超参数，评估这分类器的准确度和召回率"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
